//
//  Mine_ProgrammeViewController.m
//  fxxiaoke
//
//  Created by 鞠超 on 2016/10/26.
//  Copyright © 2016年 jc. All rights reserved.
//

#import "Mine_ProgrammeViewController.h"

#import "Mine_ProgrammeCollectionViewCell.h"
#import "Mine_ProgrammeTableViewCell.h"
#import "Add_ProgrammeViewController.h"
#import "Programme_DetailViewController.h"

@interface Mine_ProgrammeViewController () <UITableViewDelegate, UITableViewDataSource ,UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong) UIView * view_Head;

@property (nonatomic, strong) UICollectionView * collectionView;

@property (nonatomic, strong) UITableView * tableView;

@property (nonatomic, strong) NSMutableArray * arr_date;

//
@property (nonatomic, assign) NSInteger save_Index;
@property (nonatomic, assign) NSInteger now_Index;

@end

@implementation Mine_ProgrammeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self p_navi];
    
    [self p_week];
    
    [self p_data];
    
    [self p_detail];
    
    [self p_setupView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - navi
- (void )p_navi
{
    _btnLeft.hidden = YES;
    _lblLeft.hidden = YES;
    _btnRight.hidden = YES;
    _lblRight.hidden = YES;
    
    _lblTitle.text = @"2016年10月";
    _topView.backgroundColor = navi_bar_bg_color;
    
    //左返回
    UIImageView * image_LeftRetrun = [[UIImageView alloc] initWithFrame:CGRectMake(11, 31.5, 25, 25)];
    image_LeftRetrun.userInteractionEnabled = YES;
    image_LeftRetrun.image = [UIImage imageNamed:@"Left-return.png"];
    [_topView addSubview:image_LeftRetrun];
    
    UILabel * lbl_return = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(image_LeftRetrun.frame) - 6, 35, 35, 15)];
    lbl_return.text = @"我";
    lbl_return.textColor = [UIColor whiteColor];
    lbl_return.font = [UIFont systemFontOfSize:17];
    [_topView addSubview:lbl_return];
    
    UIButton * btn_return = [UIButton buttonWithType:(UIButtonTypeSystem)];
    btn_return.frame = CGRectMake(10, 22, 60, 40);
    [_topView addSubview:btn_return];
    [btn_return addTarget:self action:@selector(btn_returnAction:) forControlEvents:(UIControlEventTouchUpInside)];
    
    UIImageView * image_Setting = [[UIImageView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH - 30, 32, 20, 20)];
    image_Setting.image = [UIImage imageNamed:@"Add_white.png"];
    [_topView addSubview:image_Setting];
    
    UIButton * btn_Setting = [UIButton buttonWithType:(UIButtonTypeSystem)];
    btn_Setting.frame = CGRectMake(SCREEN_WIDTH - 10 - 30, 24.5, 30, 35);
    [_topView addSubview:btn_Setting];
    [btn_Setting addTarget:self action:@selector(SettingAction:) forControlEvents:(UIControlEventTouchUpInside)];
    
    //
    self.save_Index = 90;
    self.now_Index = 12;
}

//左返回
- (void )btn_returnAction:(UIButton *)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

//点击添加事件
- (void)SettingAction:(UIButton *)sender
{
//    NSLog(@"点击添加事件");
    
    Add_ProgrammeViewController * add_ProgrammeViewController = [[Add_ProgrammeViewController alloc] init];
    [self showViewController:add_ProgrammeViewController sender:nil];
}

//显示tabbar
- (void )viewWillAppear:(BOOL)animated
{
    [(AppDelegate *)[[UIApplication sharedApplication] delegate] hiddenTabBar];
}

#pragma mark - 布局
- (void )p_setupView
{
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
}

#pragma mark - 周一到周日
- (void )p_week
{
    self.view_Head = [[UIView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, 40)];
    self.view_Head.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [self.view addSubview:self.view_Head];
    
    UIView * viwe_line = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.view_Head.frame), SCREEN_WIDTH, 1)];
    viwe_line.backgroundColor = [UIColor grayColor];
    viwe_line.alpha = 0.5;
    [self.view addSubview:viwe_line];
    
    CGFloat length_X = SCREEN_WIDTH / 7;
    
    for (int i = 0; i < 7; i ++)
    {
        UILabel * lbl_week = [[UILabel alloc] initWithFrame:CGRectMake(length_X * i, 0, length_X, 40)];
        lbl_week.tag = 100 + i;
        lbl_week.textAlignment = NSTextAlignmentCenter;
        lbl_week.textColor = [UIColor grayColor];
        lbl_week.font = [UIFont systemFontOfSize:14.0f];
        [self.view_Head addSubview:lbl_week];
    }
    
    UILabel * lbl_1 = [self.view viewWithTag:100];
    lbl_1.frame = CGRectMake(0, 0, length_X - 6, 40);
    lbl_1.text = @"周日";
    
    UILabel * lbl_2 = [self.view viewWithTag:101];
    lbl_2.text = @"周一";
    
    UILabel * lbl_3 = [self.view viewWithTag:102];
    lbl_3.text = @"周二";
    
    UILabel * lbl_4 = [self.view viewWithTag:103];
    lbl_4.text = @"周三";
    
    UILabel * lbl_5 = [self.view viewWithTag:104];
    lbl_5.text = @"周四";
    
    UILabel * lbl_6 = [self.view viewWithTag:105];
    lbl_6.text = @"周五";
    
    UILabel * lbl_7 = [self.view viewWithTag:106];
    lbl_7.frame = CGRectMake(length_X * 6 + 5, 0, length_X - 5, 40);
    lbl_7.text = @"周六";
}

#pragma mark - 日期
- (void )p_data
{
//    self.arr_date = @[@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10",@"11",@"12",@"13",@"14",@"15",@"16",@"17",@"18",@"19",@"20",@"21",@"22",@"23",@"24",@"25",@"26",@"27",@"28",@"29",@"30",@"31",@"32",@"33",@"34",@"35",@"36",@"37",@"38",@"39",@"40",@"41",@"42"].mutableCopy;
    
    UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc] init];
    //每个item的大小
    CGFloat  item_length = (SCREEN_WIDTH - 60) / 7;
    layout.itemSize = CGSizeMake(item_length, 30);
    layout.sectionInset = UIEdgeInsetsMake(10, 0, 0, 0);
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;

    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 64 + 41, SCREEN_WIDTH, 240) collectionViewLayout:layout];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.backgroundColor = [UIColor whiteColor];
    self.collectionView.scrollEnabled = YES;
    self.collectionView.pagingEnabled = YES;
    self.collectionView.showsVerticalScrollIndicator = NO;
    [self.view addSubview:self.collectionView];
    
    //
    [self.collectionView registerClass:[Mine_ProgrammeCollectionViewCell class] forCellWithReuseIdentifier:@"cell_date"];
}

#pragma mark - CollectionView的代理
//有几个分区
- (NSInteger )numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 4;
}
//每个分区有多少个
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 42;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    Mine_ProgrammeCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell_date" forIndexPath:indexPath];
    
    cell.lbl_date.tag = indexPath.row + indexPath.section * 1000 + 1;
    cell.lbl_date.text = [NSString stringWithFormat:@"%ld",indexPath.row + 1];
    cell.lbl_date.textColor = [UIColor grayColor];
    cell.lbl_date.backgroundColor = [UIColor clearColor];
    cell.lbl_date.layer.borderColor = [UIColor clearColor].CGColor;
    cell.lbl_date.layer.borderWidth = 1.0f;
    
    if(self.save_Index != 90)
    {
        if(indexPath.section == self.save_Index / 1000 && indexPath.row == self.save_Index % 1000 - 1)
        {
            cell.lbl_date.textColor = [UIColor whiteColor];
            cell.lbl_date.backgroundColor = [UIColor orangeColor];
        }
    }
    
    if(self.now_Index == indexPath.row && indexPath.section == 0)
    {
        if(indexPath.section == self.save_Index / 1000 && indexPath.row == self.save_Index % 1000 - 1)
        {
            cell.lbl_date.textColor = [UIColor whiteColor];
            cell.lbl_date.backgroundColor = [UIColor orangeColor];
        }
        else
        {
            cell.lbl_date.layer.borderColor = [UIColor orangeColor].CGColor;
            cell.lbl_date.layer.borderWidth = 1.0f;
            cell.lbl_date.backgroundColor = [UIColor groupTableViewBackgroundColor];
        }
    }
    
    
    return cell;
}

- (void )collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if(self.save_Index != 90)
    {
        if(self.now_Index == self.save_Index % 1000 - 1 && indexPath.section == 0)
        {
            UILabel * lbl_oldDate = [self.view viewWithTag:self.save_Index];
            
            lbl_oldDate.layer.borderColor = [UIColor orangeColor].CGColor;
            lbl_oldDate.layer.borderWidth = 1.0f;
            lbl_oldDate.backgroundColor = [UIColor groupTableViewBackgroundColor];
            lbl_oldDate.textColor = [UIColor grayColor];
        }
        else
        {
            UILabel * lbl_oldDate = [self.view viewWithTag:self.save_Index];
            lbl_oldDate.backgroundColor = [UIColor clearColor];
            lbl_oldDate.textColor = [UIColor grayColor];
        }
    }
    
    self.save_Index = indexPath.row + indexPath.section * 1000 + 1;

    UILabel * lbl_date = [self.view viewWithTag:indexPath.row + indexPath.section * 1000 + 1];
    lbl_date.backgroundColor = [UIColor orangeColor];
    lbl_date.textColor = [UIColor whiteColor];
    
    dispatch_async(dispatch_get_main_queue(), ^{
        //刷新tableView(记住,要更新放在主线程中)
        [self.tableView reloadData];
    });
}

#pragma mark - 事件
- (void )p_detail
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.collectionView.frame) + 10, SCREEN_WIDTH, SCREEN_HEIGHT - CGRectGetMaxY(self.collectionView.frame) - 10) style:(UITableViewStylePlain)];
    self.tableView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.tableFooterView = [[UIView alloc] init];
    self.tableView.showsVerticalScrollIndicator = NO;
    [self.view addSubview:self.tableView];
    
    //注册
    [self.tableView registerClass:[Mine_ProgrammeTableViewCell class] forCellReuseIdentifier:@"cell_Programme"];
}

#pragma mark - tableview代理
- (NSInteger )numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger )tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (CGFloat )tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60.0f;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    Mine_ProgrammeTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell_Programme" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
}

- (void )tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//    NSLog(@"跳转详情页");
    
    Programme_DetailViewController * programme_DetailViewController = [[Programme_DetailViewController alloc] init];
    [self showViewController:programme_DetailViewController sender:nil];
}


#pragma mark - 懒加载
- (NSMutableArray *)arr_date
{
    if(_arr_date == nil)
    {
        self.arr_date = [NSMutableArray array];
    }
    
    return _arr_date;
}

@end
